import os
import time
from subprocess import call
#GIT_BASE_DIR = 'F:\myremote_note\git_note\note_for_myself_siyuan'
GIT_BASE_DIR = 'F:\\test\\note_test'
gitdate = time.strftime("%Y%m%d%H%M%S", time.localtime())
GIT_DIR_DATA = GIT_BASE_DIR + '\data\.siyuan\conf.json'
choice_list = ['更新','上传']
def git_init():
    os.chdir(GIT_BASE_DIR)
    git_init_cmd = "git init ."
    git_add_cmd = "git add -A"
    git_commit_cmd = "git commit -m {}".format(gitdate)
    git_pull_cmd = "git pull origin siyuan:siyuan"
    git_push_cmd = "git push origin siyuan:siyuan"
    call(
        git_init_cmd + "&&" +
        git_add_cmd + "&&" +
        git_commit_cmd + "&&" +
        git_pull_cmd + "&&" +
        git_push_cmd,
        shell=True
    )

def git_update():
    os.chdir(GIT_BASE_DIR)
    git_pull_cmd = "git pull origin siyuan:siyuan"
    call(
        git_pull_cmd,
        shell=True
    )

def file_or_open():
    try:
        with open(GIT_DIR_DATA, 'r') as f:
            context = f.read()
        return context
    except PermissionError as error:
        return False

if __name__ == "__main__":
    while True:
        context = file_or_open()
        if context:
            for num, char in enumerate(choice_list):
                print(num, char)
            while True:
                git_num = input('配置修正已完成，请按照以上提示输入相应的序号:')
                if git_num == '0':
                    git_update()
                    time.sleep(5)
                    break
                if git_num == '1':
                    git_init()
                    time.sleep(5)
                    break
                else:
                    print('输入错误，请重新输入')
            break
        else:
            print('正在尝试打开并修正配置文件，请稍等……')
            time.sleep(5)